# -*- coding: utf-8 -*-  

"""
Created at March 20, 2021
@author: Bob Deng
"""

"""
This is an example for base convertion with stack
"""

import sys
import getopt
from stack import Stack

def base_convert(x:int, base:int=2) -> str:
    """
    Conversion function, default is binary conversion
    Args:
        x: converted number
        base: base number
    Return:
        result_str: coverted result output as string 
    """
    
    remain_stack = Stack()
    
    while x > 0:
        remain = x % base
        remain_stack.push(remain)
        x = x // base

    result_str = ""
    while not remain_stack.isEmpty():
        result_str = result_str + str(remain_stack.pop())

    return result_str

def usage():
    print("Parameters error!")
    print("base_convert x base, example:")
    print("base_convert 128 8")

def main(argv=None):
    if argv is None:
        argv = sys.argv[1:]
      
    if len(argv) < 2 or len(argv) > 2:
        usage()
        return 0

    x = int(argv[0])
    base = int(argv[1])
    converted_str = base_convert(x, base)

    print("x: " + str(x) + " base: " + str(base) + " Converted result: " + converted_str)


if __name__ == '__main__':

    sys.exit(main())

